home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 417_01 / libftp / FtpConnect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-24  |  1.8 KB  |  77 lines

  1. /*
  2.               Library for ftpd clients.(libftp)
  3.             Copyright by Oleg Orel
  4.              All rights reserved.
  5.             
  6. This  library is desined  for  free,  non-commercial  software  creation. 
  7. It is changeable and can be improved. The author would greatly appreciate 
  8. any  advises, new  components  and  patches  of  the  existing  programs.
  9. Commercial  usage is  also  possible  with  participation of it's author.
  10.  
  11.  
  12.  
  13. */
  14.  
  15. #include "FtpLibrary.h"
  16.  
  17. STATUS FtpConnect(FTP **con,char * hostname)
  18. {
  19.   struct sockaddr_in unit;
  20.   register struct hostent *host;
  21.   register struct servent *service;
  22.   register int sock;
  23.   String S1;
  24.   STATUS x;
  25.   
  26.   *con = ( FTP * ) malloc ( sizeof (FTP));
  27.   
  28.   if ((host=FtpGetHost(hostname))==NULL)
  29.     return EXIT((*con),QUIT);
  30.   if ((service=(struct servent *) getservbyname("ftp","tcp"))==0)
  31.     return EXIT((*con),QUIT);
  32.   
  33.   unit.sin_family = host -> h_addrtype;
  34.   
  35.   bcopy(host-> h_addr_list[0],(char *)&unit.sin_addr,host->h_length);
  36.   if ( ( sock = socket ( unit.sin_family , SOCK_STREAM , 0)) < 0)
  37.     return EXIT((*con),QUIT);
  38.  
  39.   unit.sin_port = service -> s_port;
  40.  
  41.   while ( connect ( sock , &unit , sizeof unit ) < 0 )
  42.     {
  43.       host -> h_addr_list ++;
  44.       if (host -> h_addr_list[0]==NULL) {
  45.     close(sock);
  46.     return EXIT((*con),QUIT);
  47.       }
  48.       bcopy(host -> h_addr_list[0],(char *)&unit,host->h_length);
  49.       close(sock);
  50.       if ( ( sock = socket ( unit.sin_family , SOCK_STREAM , 0)) < 0)
  51.     {
  52.       close(sock);
  53.       return EXIT((*con),QUIT);
  54.     }
  55.     }
  56.   
  57.   (*con) -> sock = sock;
  58.   (*con) -> mode = 'A';
  59.   (*con) -> data = 0;
  60.   (*con) -> func = NULL;
  61.   (*con) -> debug = NULL;
  62.  
  63.   if (ftplib_debug) FtpDebug(*con);
  64.  
  65.   if ( (x=FtpGetMessage(*con,S1)) == QUIT )
  66.     return EXIT((*con),QUIT);
  67.   if ( ! FtpGood(x,120,220,EOF))
  68.     {
  69.       close(sock);
  70.       return EXIT((*con),-x);
  71.     }
  72.   return EXIT((*con),x);
  73. }
  74.  
  75.  
  76.  
  77.